<?php
namespace org;
use DirectoryIterator;
use ReflectionClass;
use ReflectionMethod;
use RuntimeException;
/**
 *  @file : ThinkActionIterator.php
 *  @brief: ThinkPHP控制器、方法遍历
 */
class ThinkActionIterator {

  private $ActionSuffix = '';
  
  public function __construct() {
    $ActionPrefix = config('use_action_prefix');
    $ActionSuffix = config('action_suffix');
    if (!($ActionPrefix === false && is_string($ActionSuffix) && !empty($ActionSuffix))) {
      throw new RuntimeException('未满足方法前缀后缀要求!');
    }
    $this->ActionSuffix = $ActionSuffix;
  }
  
  public function BianLi_Controller($RootDir) {
    $files = [];
    $Iterator = new DirectoryIterator($RootDir);
    foreach($Iterator as $FileInfo) {
      if (!$FileInfo->isDot()) {
        $path = $FileInfo->getPathname();
        if ($FileInfo->isFile()) {
          $extn = $FileInfo->getExtension();
          $name = $FileInfo->getFilename();
          $name = str_replace(".{$extn}", '', $name);
          $eax = strrpos($name, 'Controller');
          if (strcasecmp($extn, 'php') === 0 && ((strlen($name) - 10) == $eax)) {
            $files[] = $path;
          }
        }
        if ($FileInfo->isDir()) {
          $eax = $this->BianLi_Controller($path);
          if(!empty($eax)){
            $files = array_merge($files, $eax);
          }
        }
      }
    }
    return $files;
  }
  
  public function BianLi_Controller_Action($ClassName, $ClassNameBase = 'app\houtai\controller\BaseController') {
    $retn = [];
    $refl = new ReflectionClass($ClassName);
    $base = $refl->getParentClass();
    if ($base->name == $ClassNameBase) {
      $list = $refl->getMethods(ReflectionMethod::IS_PUBLIC);
      foreach($list as $item) {
        if ($item->class == $ClassName) {
          $retn[] = $item->class.'::'.$item->name;
        }
      }
    }
    return $retn;
  }
  
}
